package com.lingkun.demo.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingkun.demo.contants.uuid.IdUtils;
import com.lingkun.demo.sys.entity.RoleMenu;
import com.lingkun.demo.sys.entity.SysRole;
import com.lingkun.demo.sys.entity.sysVo.RoleVo;
import com.lingkun.demo.sys.mapper.RoleMenuMapper;
import com.lingkun.demo.sys.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingkun.demo.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小龙
 * @since 2021-04-24
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Autowired
    private  RoleMenuService roleMenuService;

    @Autowired
    private SysRoleService sysRoleService;


    @Override  //获取每个权限对应的角色
    public Map<String,List<String>> role_menu() {
        List<RoleMenu> roleMenus = baseMapper.selectList(null);
       Map<String,List<String>> map = new HashMap<>();
        for (int i = 0; i < roleMenus.size(); i++) {
            List<String> aa = new ArrayList<>();
            aa.add(roleMenus.get(i).getRoleId());
            if (map.containsKey(roleMenus.get(i).getMenuId())) {
                aa.addAll(map.get(roleMenus.get(i).getMenuId()));
            }
            map.put(roleMenus.get(i).getMenuId(), aa);
        }
        return map;
    }

    @Transactional
    @Override      // 角色的基本信息， 分配角色的路由
    public Boolean updateRoleMenu(String roleId, RoleVo roleVo) {
       Long start =  System.currentTimeMillis();
        SysRole sysRole =  new SysRole();
        //sysRole.setRoleId(roleVo.getRoleId());
        sysRole.setRoleDesc(roleVo.getRoleDesc());
        sysRole.setRoleName(roleVo.getRoleName());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
           queryWrapper.eq("role_id",roleId);
            sysRoleService.update(sysRole,queryWrapper);
            baseMapper.deleteByIds(roleId);
           List<RoleMenu>  list   =  new ArrayList<>()  ;
              for(int i =0;i<roleVo.getRoutes().size();i++){
                  RoleMenu roleMenu = new RoleMenu();
                     roleMenu.setRoleId(roleId);
                     roleMenu.setMenuId(roleVo.getRoutes().get(i).getMenuId());
                     list.add(roleMenu);
              }
           Boolean flag = roleMenuService.saveBatch(list);
        Long end =  System.currentTimeMillis();
        System.out.println(end-start);
        return flag;
    }

    @Override     //创建角色及其分配角色的权限
    public Boolean addRole(RoleVo roleVo) {
        SysRole sysRole = new SysRole();
        String id =  IdUtils.simpleUUID();
        sysRole.setRoleId(id);
          sysRole.setRoleName(roleVo.getRoleName());
          sysRole.setRoleDesc(roleVo.getRoleDesc());
        sysRoleService.save(sysRole);
        List<RoleMenu>  list   =  new ArrayList<>()  ;
        for(int i =0;i<roleVo.getRoutes().size();i++){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(id);
            roleMenu.setMenuId(roleVo.getRoutes().get(i).getMenuId());
            list.add(roleMenu);
        }
        Boolean flag = roleMenuService.saveBatch(list);
        return flag;
    }

    @Override  //通过角色id删除角色宇权限表的关联数据
    public Boolean removeRoleMenu(String roleId) {
        baseMapper.deleteByIds(roleId);
        return true;
    }


}
